package com.amigo.online.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.param.result.LocalUserInfo;
import com.amigo.online.message.mapper.online.UserOneSignalMapper;
import com.amigo.online.message.mybatisbean.UserOneSignal;
import com.amigo.online.message.mybatisbean.UserOneSignalExample;
import com.amigo.online.message.service.NoticeService;
import com.amigo.online.common.core.response.Result;
import com.amigo.online.message.util.onesignal.OneSignalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    UserOneSignalMapper userOneSignalMapper;
    @Autowired
    OneSignalUtil oneSignalUtil;
    @Override
    public Result updateUserNoticeId(String oneSignalId, String token){

        LocalUserInfo localUserInfo = LocalUser.getUser();
        Long userId = localUserInfo.getUser().getId();
        UserOneSignalExample userOneSignalExample = new UserOneSignalExample();
        userOneSignalExample.createCriteria().andUserIdEqualTo(userId);
        List<UserOneSignal> list = userOneSignalMapper.selectByExample(userOneSignalExample);
        UserOneSignal userOneSignal = null;
        if(list.size()!=0){
            userOneSignal = list.get(0);
        }
        if(list.size()==0){
            userOneSignal = new UserOneSignal();
            userOneSignal.setUserId(userId);
            userOneSignal.setOneSignalId(oneSignalId);
            userOneSignal.setUpdateTime(new Date());
            userOneSignal.setCreateTime(new Date());
            userOneSignalMapper.insertSelective(userOneSignal);
            return Result.success();
        }else if(oneSignalId.equals(userOneSignal.getOneSignalId())){
            return Result.success();
        }else {
            userOneSignal.setOneSignalId(oneSignalId);
            userOneSignal.setUpdateTime(new Date());
            userOneSignalMapper.updateByPrimaryKey(userOneSignal);
            return Result.success();
        }
    }

//
//    public Result pushByOneSignalId(Long userId, String message) {
////        String userId = Long.valueOf(map.get("contents"));
//
//
//        UserOneSignalExample userOneSignalExample = new UserOneSignalExample();
//        userOneSignalExample.createCriteria().andUserIdEqualTo(userId);
//        List<UserOneSignal> list = userOneSignalMapper.selectByExample(userOneSignalExample);
//        UserOneSignal userOneSignal = list.get(0);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("en", message);
//        oneSignalUtil.sendByUserId(jsonObject ,userId);
//        return Result.success();
//    }




}
